<template>
    <el-form :model="form" status-icon :rules="rules" :style="formStyle"  ref="form" label-width="100px">
        <el-form-item label="帐号" prop="username">
            <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm()" :loading="logining">登录</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import {handleToken,clientInfo} from './HandleToken'
    import qs from 'qs'
    import { Base64 } from 'js-base64'
    export default {
        name: "Login",
        props: {
            formStyle:String
        },
        data (){
            return {
                logining: false,
                form:{
                    username: null,
                    password: null
                },
                rules: {
                    username: [
                        {required: true, message: '帐号不能为空'}
                    ],
                    password: [
                        {required: true, message: '密码不能为空'}
                    ]
                }
            }
        },
        methods: {
            submitForm() {
                let self = this
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        self.logining = true
                        self.axios.post(self.$baseURL.auth +  '/oauth/token?scope=read&grant_type=password',
                            qs.stringify(self.form),
                            {
                                headers:{
                                    'Content-Type': 'application/x-www-form-urlencoded',
                                    Authorization: 'Basic ' + Base64.encode(clientInfo.clientId + ':' + clientInfo.clientSecret)
                                }
                            }).then(res => {
                            self.$message.success('登录成功')
                            handleToken(res)
                            self.$emit('success')

                        }).catch(error => {
                            if (error.response) {
                                if(error.response.status == 401){
                                    self.$message.error("用户名或密码不正确")
                                    return
                                }
                                self.$message.error(error.response.data.msg)
                            }else{
                                self.$message.error('网络错误或服务不可用')
                            }
                        }).finally(()=>{
                            self.logining = false
                        })
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped>

</style>